package com.bunde.repo.com.bunde.service;

import com.bunde.repo.model.InventoryModel;
import org.junit.Test;

import java.sql.Array;
import java.util.*;

public class InventoryService {
    // 测试数据
    List<InventoryModel> data = new ArrayList<>();


    public InventoryService(){
        // 测试数据
        data.add(new InventoryModel(1, "独立站", "中国", 2021, 200));
        data.add(new InventoryModel(1, "独立站", "中国", 2022, 300));
        data.add(new InventoryModel(1, "独立站", "中国", 2023, 400));
        data.add(new InventoryModel(1, "独立站", "中国", 2024, 500));
        data.add(new InventoryModel(1, "独立站", "中国", 2025, 600));
        data.add(new InventoryModel(1, "独立站", "中国", 2026, 500));
        data.add(new InventoryModel(1, "独立站", "中国", 2027, 400));
        data.add(new InventoryModel(1, "独立站", "中国", 2028, 300));
        data.add(new InventoryModel(1, "独立站", "中国", 2029, 200));

        data.add(new InventoryModel(2, "亚马逊", "美国", 2021, 300));
        data.add(new InventoryModel(2, "亚马逊", "美国", 2022, 300));
        data.add(new InventoryModel(2, "亚马逊", "美国", 2023, 300));
        data.add(new InventoryModel(2, "亚马逊", "美国", 2024, 300));
        data.add(new InventoryModel(2, "亚马逊", "美国", 2025, 300));
        data.add(new InventoryModel(2, "亚马逊", "美国", 2026, 300));
        data.add(new InventoryModel(2, "亚马逊", "美国", 2027, 300));
        data.add(new InventoryModel(2, "亚马逊", "美国", 2028, 300));
        data.add(new InventoryModel(2, "亚马逊", "美国", 2029, 300));

        data.add(new InventoryModel(3, "亚马逊", "德国", 2021, 400));
        data.add(new InventoryModel(3, "亚马逊", "德国", 2022, 400));
        data.add(new InventoryModel(3, "亚马逊", "德国", 2023, 400));
        data.add(new InventoryModel(3, "亚马逊", "德国", 2024, 400));
        data.add(new InventoryModel(3, "亚马逊", "德国", 2025, 400));
        data.add(new InventoryModel(3, "亚马逊", "德国", 2026, 400));
        data.add(new InventoryModel(3, "亚马逊", "德国", 2027, 400));
        data.add(new InventoryModel(3, "亚马逊", "德国", 2028, 400));
        data.add(new InventoryModel(3, "亚马逊", "德国", 2029, 400));
    }

    // 讲数据转换为excel表的格式。并查集或者直接Mp进行统计
    // 返回数据：
    // list[0]: InventoryModel, list[1-end] = startWeek-endWeek
    public List<List<Object>> getInventoryData(int startWeek, int endWeek){
        Map<Long, List<InventoryModel>> mp = new TreeMap<>();
        for(int i = 0; i < data.size(); i++){
            if(data.get(i).getWeekDate() < startWeek || data.get(i).getWeekDate() > endWeek)continue;
            if (mp.containsKey(data.get(i).getSiteId())){
                mp.get(data.get(i).getSiteId()).add(data.get(i));
            }else{
                List<InventoryModel> ls = new ArrayList<>();
                ls.add(data.get(i));
                mp.put(data.get(i).getSiteId(), ls);
            }
        }
        // 其他需要的排序处理
        // TODO

        // 封装成最终对应excel表的数据
        List<List<Object>> res = new ArrayList<>();
        for(Map.Entry<Long, List<InventoryModel>>entry : mp.entrySet()) {
            List<Object> lines = new ArrayList<>();
            List<InventoryModel> list = entry.getValue();
            lines.add(list.get(0));
            for (InventoryModel inventoryModel : list) {
                lines.add(inventoryModel.getNum());
            }
            res.add(lines);
        }
        return res;
    }

    @Test
    public void test(){
        List<List<Object>> inventoryData = getInventoryData(2022, 2028);
        for (int i = 0; i < inventoryData.size(); i++) {
            List<Object> objects = inventoryData.get(i);
            for (int j = 0; j < objects.size(); j++) {
                if(j == 0){
                    System.out.print(((InventoryModel)objects.get(j)) + " ");
                }else {
                    System.out.print(((Integer) objects.get(j)) + " ");
                }
            }
            System.out.println();
        }
    }
}
